package com.aipp.mall.h5.service;


import com.aipp.mall.h5.domain.entity.Product;
import com.aipp.mall.h5.domain.entity.Sku;
import com.aipp.mall.h5.domain.query.ProductQuery;
import com.aipp.mall.h5.domain.vo.H5ProductVO;
import com.aipp.mall.h5.domain.vo.ProductDetailVO;
import com.aipp.mall.h5.mapper.BrandMapper;
import com.aipp.mall.h5.mapper.ProductMapper;
import com.aipp.mall.h5.mapper.SkuMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.stereotype.Service;

/**
 * 商品信息Service业务层处理
 *
 * @author zcc
 */
@Service
@Slf4j
public class ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private SkuMapper skuMapper;

    @Resource
    private BrandMapper brandMapper;


    /**
     * 查询商品信息列表
     *
     * @param query 查询条件
     * @return 商品信息
     */
    public IPage<H5ProductVO> selectList(ProductQuery query) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(query.getOrderField()) && StrUtil.isNotEmpty(query.getOrderSort())) {
            if ("desc".equalsIgnoreCase(query.getOrderSort())) {
                qw.orderByDesc(query.getOrderField());
            } else {
                qw.orderByAsc(query.getOrderField());
            }
        } else {
            qw.orderByDesc("publish_status");
            qw.orderByAsc("sort");
        }
        qw.eq(query.getCategoryId() != null, "category_id", query.getCategoryId());
        qw.eq(query.getPublishStatus() != null, "publish_status", query.getPublishStatus());
        qw.like(StrUtil.isNotEmpty(query.getSearch()), "name", "%".concat(query.getSearch().trim()).concat("%"));
        qw.notIn(CollUtil.isNotEmpty(query.getExcludeProductIds()), "id", query.getExcludeProductIds());
        qw.in(CollUtil.isNotEmpty(query.getIds()), "id", query.getIds());
        Page<Product> productPage = productMapper.selectPage(Page.of(query.getPage(), query.getSize()), qw);
        if (productPage.getRecords().isEmpty()) {
            return new Page<>();
        }
        return productPage.convert((product -> BeanUtil.copyProperties(product, H5ProductVO.class)));
    }

    public ProductDetailVO queryDetail(Long id) {
        ProductDetailVO res = new ProductDetailVO();
        Product d = productMapper.selectById(id);
        res.setProduct(d);
        LambdaQueryWrapper<Sku> qw = new LambdaQueryWrapper<>();
        qw.eq(Sku::getProductId, id);
        res.setSkus(skuMapper.selectList(qw));
        if (d.getBrandId() != null) {
            res.setBrand(brandMapper.selectById(d.getBrandId()));
        }
        return res;
    }
}
